package restaurant.iterators;

import java.util.ArrayList;
import java.util.NoSuchElementException;

import restaurant.Table;

/**
 * An iterator to view the empty tables. An empty table is a table that is not
 * occupied. It checks each table to determine whether or not it has been
 * assigned. If it hasn't then it gets returned.
 * 
 * @author Odessa
 */
public class EmptyTablesIterator implements Iterator
{
	/** The table iterator. */
	private java.util.Iterator<Table> table_iterator;

	/** The next table. */
	private Table nextTable;



	/**
	 * Instantiates a new empty tables iterator.
	 * 
	 * @param tables the tables
	 */
	public EmptyTablesIterator(ArrayList<Table> tables) {
		table_iterator = tables.iterator();
	}



	/**
	 * Determines if there is another empty table.
	 * 
	 * @return {@code true} if there is another empty table.
	 * 
	 * @see restaurant.iterators.Iterator#hasNext()
	 */
	@Override
	public boolean hasNext()
	{
		boolean matchFound = false;

		while (table_iterator.hasNext()) {
			Table temp = (Table) table_iterator.next();

			if (!temp.isAssigned()) {
				matchFound = true;
				nextTable = temp;
				break;
			}
		}

		if (matchFound == true) {
		}
		else
			nextTable = null;

		return matchFound;
	}



	/**
	 * Returns the next empty table.
	 * 
	 * @return the next empty table.
	 * @see restaurant.iterators.Iterator#next()
	 */
	@Override
	public Object next()
	{
		if (nextTable == null) {
			throw new NoSuchElementException("There are no more empty tables.");
		}
		else
			return nextTable;
	}

}
